package servlet;

import util.Linksql;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

public class SignIn extends HttpServlet {
    String idstatus;
    String idtext;
    int one;
    int two;
    int three;
    int four;
    int five;
    int six;
    String sqlone = "update book_s_rd set arrivetime=? ,status=? where stuid=? and status=?";
    String sqltwo = "update book_s_sn set arrivetime=? ,status=? where stuid=? and status=? ";
    String sqlthree = "update book_s_st set arrivetime=? , status=? where stuid=? and status=? ";
    String sqlfour = "update book_t_rd set arrivetime=? , status=? where teacherid=? and status= ?";
    String sqlfive = "update book_t_sn set arrivetime=? , status=? where teacherid=? and status= ?";
    String sqlsix = "update book_t_st set arrivetime=? , status=? where teacherid=? and status= ?";
    String sqlone1 = "update book_s_st set actualendtime=? , status=? where stuid=? and status=?";
    String sqltwo1="update book_s_st set actualendtime=? , status=? where stuid=? and status=?";
    String sqlthree1 = "update book_s_st set actualendtime=? , status=? where stuid=? and status=? ";
    String sqlfour1 = "update book_t_rd set actualendtime=? , status=? where teacherid=? and status= ?";
    String sqlfive1 = "update book_t_sn set actualendtime=? , status=? where teacherid=? and status= ?";
    String sqlsix1 = "update book_t_st set actualendtime=? , status=? where teacherid=? and status= ?";
    String time;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            java.util.Date d=new Date();
            Timestamp date=new Timestamp(d.getTime());
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            //time=sdf.format(new Date(System.currentTimeMillis()));
            //Timestamp date=new Timestamp(new java.util.Date(System.currentTimeMillis()));
            Connection con = Linksql.getConnection();
            PreparedStatement pre;
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            idstatus = request.getParameter("idstatus");
            idtext = request.getParameter("idtext");
            //签到
            if (idstatus.equals("签到")) {
                pre = con.prepareStatement(sqlone);
                pre.setTimestamp(1,date);
                pre.setString(2,"已签到");
                pre.setString(3,idtext);
                pre.setString(4,"未签到");
                one=pre.executeUpdate();

                pre = con.prepareStatement(sqltwo);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签到");
                pre.setString(3,idtext);
                pre.setString(4,"未签到");
                two=pre.executeUpdate();

                pre = con.prepareStatement(sqlthree);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签到");
                pre.setString(3,idtext);
                pre.setString(4,"未签到");
                three=pre.executeUpdate();

                pre = con.prepareStatement(sqlfour);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签到");
                pre.setString(3,idtext);
                pre.setString(4,"未签到");
                four=pre.executeUpdate();

                pre = con.prepareStatement(sqlfive);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签到");
                pre.setString(3,idtext);
                pre.setString(4,"未签到");
                five=pre.executeUpdate();

                pre = con.prepareStatement(sqlsix);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签到");
                pre.setString(3,idtext);
                pre.setString(4,"未签到");
                six=pre.executeUpdate();
                if((one==0)&&(two==0)&&(three==0)&&(four==0)&&(five==0)&&(six==0)){
                    response.getWriter().write("签到失败");
                }else {
                    response.getWriter().write("签到成功");
                }
                pre.close();
                con.close();

            }
            //签离
            if (idstatus.equals("签离")) {
                pre = con.prepareStatement(sqlone1);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签离");
                pre.setString(3,idtext);
                pre.setString(4,"已签到");
                one=pre.executeUpdate();

                pre = con.prepareStatement(sqltwo1);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签离");
                pre.setString(3,idtext);
                pre.setString(4,"已签到");
                two=pre.executeUpdate();

                pre = con.prepareStatement(sqlthree1);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签离");
                pre.setString(3,idtext);
                pre.setString(4,"已签到");
                three=pre.executeUpdate();

                pre = con.prepareStatement(sqlfour1);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签离");
                pre.setString(3,idtext);
                pre.setString(4,"已签到");
                four=pre.executeUpdate();

                pre = con.prepareStatement(sqlfive1);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签离");
                pre.setString(3,idtext);
                pre.setString(4,"已签到");
                five=pre.executeUpdate();

                pre = con.prepareStatement(sqlsix1);
                pre.setTimestamp(1,date );
                pre.setString(2,"已签离");
                pre.setString(3,idtext);
                pre.setString(4,"已签到");
                six=pre.executeUpdate();
                if((one==0)&&(two==0)&&(three==0)&&(four==0)&&(five==0)&&(six==0)){
                    response.getWriter().write("签离失败");
                }else {
                    response.getWriter().write("签离成功");
                }
                pre.close();
                con.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
